import friendsGroupSqlite from '@/sqlite_services/friendsGroup.js'
import friendSqlite from '@/sqlite_services/friend.js'
import $store from '@/store/index.js';
export default {
	/* 分组列表 */
	async list() {
		try{
			let res = await uni.$api.friendsGroup.list()
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 分组用户列表 */
	async friendList(group_id) {
		try{
			let res = await uni.$api.friendsGroup.friendList({group_id})
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 创建 */
	async add(group_name) {
		try{
			let res = await uni.$api.friendsGroup.add({group_name})
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 修改 */
	async edit(group_id,group_name) {
		try{
			let res = await uni.$api.friendsGroup.edit({group_id,group_name})
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 删除分组 */
	async delGroup(group_id) {
		try{
			let res = await uni.$api.friendsGroup.delGroup({group_id})
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 更换分组 */
	async operateFriend(group_id,group_name,friend_id) {
		try{
			let res = await uni.$api.friendsGroup.operateFriend({group_id,friend_ids:[friend_id]})
			if(res){
				//修改userInfo表的分组信息
				friendSqlite.editUserInfo(friend_id,{
					friend_group_id: group_id,
					friend_group_name: group_name
				})
				//更改store存的这个信息
				$store.commit('updateFriendInfo',{
					friend_group_id: group_id,
					friend_group_name: group_name,
					user_id:friend_id,
				})
				//更新好友列表
				$store.dispatch('updateFriendList', { friend_idx:friend_id, data: {
					friend_group:group_id
				}});
				
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	
}